package com.gxuwz.leave.servlet;

import com.gxuwz.leave.context.AppContext;
import com.gxuwz.leave.entity.Class;
import com.gxuwz.leave.entity.Department;
import com.gxuwz.leave.service.ClassService;
import com.gxuwz.leave.service.DepService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@WebServlet(name="classServlet", urlPatterns = {"/class"})
public class ClassServlet extends BaseHttpServlet{
    private final static String VIEW_ADD = "/WEB-INF/views/class/class_add.jsp";
    private final static String VIEW_EDIT = "/WEB-INF/views/class/class_edit.jsp";
    private final static String VIEW_LIST = "/WEB-INF/views/class/class_list.jsp";
    private final static String ACTION_LIST = "/class?action=list";
    private ClassService classService;
    private DepService depService;
    @Override
    public void init() throws ServletException {
        super.init();
        classService = AppContext.getBean(ClassService.class);
        depService = AppContext.getBean(DepService.class);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");

        String view = ErrorPage_500;
        try{
            switch (action){
                //导向新增页面
                case "input":
                    view = VIEW_ADD;
                    Class clas = new Class();
                    List<Department> depList = depService.list(null);
                    req.setAttribute("class", clas);
                    req.setAttribute("depList", depList);
                    break;
                //提交新增
                case "add":
                    clas = new Class();
                    clas.setClassID(req.getParameter("classID"));
                    clas.setClassName(req.getParameter("className"));
                    clas.setDepID(req.getParameter("depID"));
                    clas.setMajor(req.getParameter("major"));
                    clas.setGrade(req.getParameter("grade"));
                    classService.addClass(clas);
                    req.setAttribute("operation", "success");
                    view = ACTION_LIST;
                    break;
                //编辑操作时，获取那一行的数据
                case "get":
                    String classID = req.getParameter("classID");
                    clas = classService.findClass(classID);
                    depList = depService.list(null);
                    view = VIEW_EDIT;
                    req.setAttribute("clas", clas);
                    req.setAttribute("depList", depList);
                    break;
                //提交编辑操作
                case "edit":
                    clas = new Class();
                    clas.setClassID(req.getParameter("classID"));
                    clas.setClassName(req.getParameter("className"));
                    clas.setDepID(req.getParameter("depID"));
                    clas.setMajor(req.getParameter("major"));
                    clas.setGrade(req.getParameter("grade"));
                    classService.editClass(clas);
                    req.setAttribute("operation", "success");
                    view = ACTION_LIST;
                    break;
                //搜索
                case "search":
                    String keywords = req.getParameter("keywords");
                    clas = new Class();
                    clas.setClassID(keywords);
                    List<Class> list = classService.list(clas);
                    if(list.isEmpty()){
                        clas = new Class();
                        clas.setClassName(keywords);
                        list =  classService.list(clas);
                    }
                    view = VIEW_LIST;
                    req.setAttribute("list", list);
                    break;
                //删除操作
                case "delete":
                    classID = req.getParameter("classID");
                    classService.deleteClass(classID);
                    req.setAttribute("operation", "success");
                    view = ACTION_LIST;
                    break;
                case "list":
                    List<Class> classList = classService.list(null);
                    view = VIEW_LIST;
                    req.setAttribute("list", classList);
                    break;
                default:
            }
        }catch (Exception e){
            e.printStackTrace();
            req.setAttribute("message", e.getMessage());
            req.setAttribute("operation", "failure");
            view = ACTION_LIST;
        }
        super.doDispatch(req,resp,view);
    }
}
